define(window.backboneCommon.getViewDefine("report.index"), function ($,require,Collection,tpl) {
    var backboneView= Backbone.View.extend({
        events: {
            "click .global-listParam":"saveGlobalListParam"
        },
        initialize: function () {
            this.listenTo(this.model, 'bind:PageList', this.bindList);
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(){
            this.elementModel={};
            this.extObject={};
            this.pageData={};
            this.elementModel.table=$("#tb_list");
            this.elementModel.timeRange=$("#time_range");
            this._initPagePower();
        },
        _initPagePower:function(){
            var self=this;
            $.ExtAssist.rolePower.operatePage("list",[],function(json){
                self.pageData.powerInfo=json;
                self._initBodyMain();
            });
        },
        _initBodyMain:function(){
            this._initAssistCallback();
            this._getPageGlobalParam();
            this._initTimeRange();
            this._initInputSearch();
            this._initPageList();
        },
        _initAssistCallback:function(){
            var self=this;
            $.ExtAssist.winUnload.callback(function(){
                self._setPageGlobalParam(null)
            });
        },
        _getPageGlobalParam:function(){
            var data=$.ExtAssist.pageListParam.getData("ReportList");
            if(data.search){$("#panel_inputSearch").data("initValue",data.search)}
            if(data.startTime){$("#time_range").data("startValue",data.startTime)}
            if(data.endTime){$("#time_range").data("endValue",data.endTime)}
            this.pageData.listParam=data;
        },
        _setPageGlobalParam:function(param){
            $.ExtAssist.pageListParam.setData("ReportList",param);
        },
        saveGlobalListParam:function(){
            var param=$.ExtCommon.getJsonClone(this.pageData.listParam);
            param.offset=this.extObject.table.getCurrentOffset();
            this._setPageGlobalParam(param);
        },
        _initTimeRange:function () {
            var self=this;
            var options={type:"range",
                addQuick:{"本月": [$.ExtMoment.initMoment().startOf('month'), $.ExtMoment.initMoment()]},
                maxDate:$.ExtMoment.initMoment(),
                changeCallback: function () {self.refreshList()}
            };
            if($.ExtCommon.isCheckUndefined(this.pageData.listParam.startTime) && $.ExtCommon.isCheckUndefined(this.pageData.listParam.endTime)){
                options.startValue=$.ExtMoment.initAdd(-1);
                options.endValue=$.ExtMoment.initAdd(-1);
            }
            self.elementModel.timeRange.ExtDatePicker(options);
        },
        _initInputSearch:function () {
            var self=this;
            self.extObject.inputSearch=$("#panel_inputSearch").ExtInputSearch({
                placeholder:"广告主ID,广告主名称",
                searchCallback:function () {self.refreshList()}
            })
        },
        _initPageList: function () {
            var self=this;
            var options={
                columns: [
                    {data: "id", class: "w-50"},
                    {data: "name", class: ""},
                    {data: "pv",class: "w-140 num-int text-right"},
                    {data: "click",class: "w-140 num-int text-right"},
                    {data: "cr", class: "w-140 num-float2 text-right"},
                    {data: "price", class: "w-140 num-float2 text-right"},
                    {data: "cost",class: "w-140 num-float2 text-right"}
                ],
                infoExt:{
                    ajaxDataPara:function(){
                        var param= {};
                        param= $.ExtCommon.initMergeJson(param,self._getSearchParam());
                        self.pageData.listParam = $.ExtCommon.getJsonClone(param);
                        return param;
                    },
                    ajaxWholeCallback:function(param){
                        self.collection.getPageList(param);
                    }
                },
                createdRow: function (row, data) {
                    var elementTd=$('td', row);
                    elementTd.eq(1).html($.ExtCommon.getStringConnect('<div><a class="btn-operate longterm global-listParam" href="#report/detail/'+data.id+'">{0}</a></div>',[data.name]));
                },
                drawCallback: function () {
                    $.ExtNumeric.initAll();
                    $.ExtAssist.urlParam.setUrl();
                }
            };
            if(!$.ExtCommon.isCheckEmpty(self.pageData.listParam.offset)){options.displayStart=self.pageData.listParam.offset}
            self.extObject.table=self.elementModel.table.ExtDataTable(options);
        },
        _getSearchParam:function(){
            var param={};
            var keyword = this.extObject.inputSearch.getValue();
            if (!$.ExtCommon.isCheckEmpty(keyword)) {param.search = keyword;}
            var arrTime=this.elementModel.timeRange.ExtDatePicker("getValue");
            if (!$.ExtCommon.isCheckEmpty(arrTime[0])) {param.startTime = arrTime[0];}
            if (!$.ExtCommon.isCheckEmpty(arrTime[1])) {param.endTime = arrTime[1];}
            return param;
        },
        bindList:function(){
            var data=this.model.get("dataPageList");
            this.extObject.table.ajaxWholeRefreshData(data);
        },
        refreshList: function () {this.extObject.table.refresh()}
    });
    var viewModel=function(){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({ el: "#container_body",collection:this.collection,model:this.model});
    };
    viewModel.prototype={
        initView:function () {
            this.view.initMain();
        }
    };
    return viewModel;
});